sync && echo 3>/proc/sys/vm/drop_caches
export https_proxy=http://20.20.20.52:3128
user=pkq
env_path=/data/ss_env/ENVFILE
workPath=/data/muyulinzhong
sourcePath=${workPath}/sharestorage
codeBase=${sourcePath}/codebase
yatPath=${sourcePath}/auto_frame
yatShell=${sourcePath}/YatShell
resultPath=${yatPath}/result
finalLog=${yatPath}/log
pipLineResult=${workPath}/result_collect/pipline-result-show

# 2024-4-2 预期：
# 整体yat用例按照文件夹进行分类，文件夹可新增；
# 采用目录方式进行整体遍历和执行，进行统计信息判断；
# 当日CI用例执行结果存放在sourcePath下的history文件夹下，以时间点进行命名

function regenerate_report()
{
	timeStr='2024-05-12_05_15_47'
    yatLogStr=".log"
    rootLogStr="_root.log"
    rm -rf $pipLineResult/History/$timeStr
    mkdir -p ${pipLineResult}/History/${timeStr}
    list=`ls $sourcePath/$timeStr | grep -v 'work'`
    for dir in $list
    do
    	echo $dir
        mkdir -p $pipLineResult/History/$timeStr/$dir
        funYatLogFile=$dir$yatLogStr
        funRootLogFile=$dir$rootLogStr
        cp $sourcePath/$timeStr/$dir/result/$dir/* $pipLineResult/History/$timeStr/$dir -r
        cp $sourcePath/$timeStr/$dir/root.log $pipLineResult/History/$timeStr/$funRootLogFile
        cp $sourcePath/$timeStr/$dir/log/yat.log $pipLineResult/History/$timeStr/$funYatLogFile
    done
    rm -rf ${workPath}/result_collect/result.md
    cd ${workPath}/result_collect
    python3 mdGen.py /data/muyulinzhong/result_collect/pipline-result-show/History/$timeStr
    mv result.md $pipLineResult/TodayResult.md
    cd $pipLineResult
    git add .
    git commit -m $timeStr || true
    git push origin master --force || true
}

function excuteSchd()
{
    echo “============ Step 2：Yat用例开始执行 ============”
    rm -rf $yatShell/result.out || true
    rm -rf $yatShell/filterOrder || true
    timeStr=`date +%Y-%m-%d_%H_%M_%S`
    mkdir -p $sourcePath/$timeStr
    # 创建历史文件
    mkdir -p ${pipLineResult}/History/${timeStr}
    local schdPath=${yatPath}/schedule
    okStr="ok "
    erStr="er "
    dotStr="/"
    yatLogStr=".log"
    shcdFile=".schd"
    rootLogStr="_root.log"
    # 按照priority生成执行顺序；
    python3 $yatShell/filter_order.py >> $yatShell/filterOrder
    # 开始进行流水线文件生成；
    while IFS= read -r line
    do
        file=$line$shcdFile
        output=""
        funName=$line
        echo "****** ${funName} 功能测试开始******"
        yat suite -s schedule/$file --timeout 1800 --color 2>/dev/null
        mkdir -p $sourcePath/$timeStr/$funName
        mkdir -p $pipLineResult/History/$timeStr/$funName
        tempStr="${funName} "
        local all_num=`find ${yatPath}/testcase/${funName} -name *ShareStorage*.py | wc -l`
        local ok_num=$(cat ${finalLog}/yat.log | grep ": ok" | wc -l)
        local er_num=$(cat ${finalLog}/yat.log | grep ": er" | wc -l)
        if [ "$all_num" = "$ok_num" ];then
            tempStr=$tempStr$okStr
        else
            tempStr=$tempStr$erStr
        fi
        output=$output$tempStr$ok_num$dotStr$all_num
        echo $output >> $yatShell/result.out
        cp $finalLog $sourcePath/$timeStr/$funName -r
        cp $resultPath $sourcePath/$timeStr/$funName -r
        cp root.log $sourcePath/$timeStr/$funName
        # 备份root.log和yat.log到该timeStr对应文件夹下，并且改名；
        funYatLogFile=$funName$yatLogStr
        funRootLogFile=$funName$rootLogStr
        cp $finalLog/yat.log $pipLineResult/History/$timeStr/$funYatLogFile
        cp $resultPath/$funName/* $pipLineResult/History/$timeStr/$funName -r
        cp root.log $pipLineResult/History/$timeStr/$funRootLogFile
        rm -rf root.log  
    done < "${yatShell}/filterOrder"

    echo "============ Step 3：Yat用例执行结束，进行结果整理 ============="
    rm -rf ${workPath}/result_collect/result.md
    cd ${workPath}/result_collect
    python3 mdGen.py /data/muyulinzhong/result_collect/pipline-result-show/History/$timeStr
    mv result.md $pipLineResult/TodayResult.md
    cd $pipLineResult
    git add .
    git commit -m $timeStr || true
    git push origin master --force || true
}

function yatPrepare()
{
    echo "============ Step 1：前瞻性准备 ============"
    echo "git 分支更新中....."
    result=`ls ${codeBase} | grep Yat` || true
    if [ -z $result ];
    then
    	cd $codeBase
        #git clone https://gitee.com/opengauss/Yat.git
        git clone https://gitee.com/zhenjinyang/Yat.git
    fi
    
    cd ${codeBase}/Yat
    git pull origin || true
    git reset --hard origin/master || true

    echo "每日用例更新拉取中......"
    cd ${yatPath}/testcase
    ls | grep -v -- utils | xargs rm -rf
    cd ${yatPath}/schedule
    rm -rf ./*.schd
    # 开始copy
    cd ${yatPath}/testcase

    # 10-16 muyulinzhong 先移除SPQ用例；
    rm -rf ${codeBase}/Yat/sharestorage/testcase/DEVELOPERS_VERIFY/SPQPLUGIN

    cp ${codeBase}/Yat/sharestorage/testcase/DEVELOPERS_VERIFY/* ./ -r
    #mv CommonSH.py ./utils/
    #mv CaseDetail  ${yatShell}
    echo "功能schd文件生成中......"
    cd ${yatShell}
    python3 case_detail_update.py     #
    python schd_file_fun.py -p ${yatPath}/testcase
    mv ./*.schd ${yatPath}/schedule/
}
function stop_cluster()
{
	set -e
    timeStr=`date +%Y-%m-%d_%H`
    su - ${user}<<EOF
        source  ${env_path}
        echo " =================  数据库日志备份   ================="
        cp -r /data/ss_env/install/data /home/pkq/log/$timeStr
        cp -r /data/ss_env/install/dss_home /home/pkq/log/$timeStr
        cp -r /data/ss_env/install/log /home/pkq/log/$timeStr    
        ssh openGauss153
        cp -r /data/ss_env/install/data /home/pkq/log/$timeStr
        cp -r /data/ss_env/install/dss_home /home/pkq/log/$timeStr
        cp -r /data/ss_env/install/log /home/pkq/log/$timeStr
        
        ssh openGauss151
        cp -r /data/ss_env/install/data /home/pkq/log/$timeStr
        cp -r /data/ss_env/install/dss_home /home/pkq/log/$timeStr
        cp -r /data/ss_env/install/log /home/pkq/log/$timeStr

        echo " =================  stop集群    ================="

        cm_ctl query -Cvipd; cm_ctl stop;        
EOF
}
function main() 
{
	cd $yatPath
    ulimit -c unlimited
    #yatPrepare
    echo "执行脚本..."
    #regenerate_report
    cd ${yatPath}
    excuteSchd
    echo "执行脚本完成"
    sshpass -p 'openGauss1#2_3$00' scp  $yatShell/result.out root@20.20.20.79:/home/songshixuan/YatShell
    stop_cluster
}

main $@
exit $?